import XEUtils from '@/js_sdk/xe-utils/xe-utils.umd.min.js'
import utils from '@/utils/index.js'
const order = {
	// 订单状态定义，这里才有方法定义，而不是属性定义，因为在vue模板里，无法通过属性访问到数据，但可以访问到方法
	status() {
		return {
			DZF: {
				code: 101,
				name: '待支付'
			},
			DFH: {
				code: 201,
				name: '待发货'
			},
			DSH: {
				code: 301,
				name: '待收货'
			},
			SYZ: {
				code: 401,
				name: '使用中'
			},
			GHZ: {
				code: 501,
				name: '归还中'
			},
			JYQX: {
				code: 601,
				name: '交易取消'
			},
			JYGB: {
				code: 701,
				name: '交易关闭'
			},
			JYWC: {
				code: 801,
				name: '交易完成'
			},
			DJS: {
				code: 901,
				name: '待结算'
			},
			// 仅落地方统计分成订单列表里展示
			YHSY: {
				code: 611,
				name: '用户爽约'
			},
			DZT: {
				code: 311,
				name: '待自提'
			},
		}
	},
	
	onlineServiceStatus() {
		return {
			DZF: {
				code: 100,
				name: '待支付'
			},
			DSY: {
				code: 200,
				name: '待使用'
			},
			YYZ: {
				code: 300,
				name: '预约中'
			},
			FWZ: {
				code: 400,
				name: '服务中'
			},
			YWC: {
				code: 500,
				name: '交易完成'
			},
			YGB: {
				code: 600,
				name: '交易关闭'
			}
		}
	},
	
	blendServiceStatus() {
		return {
			DZF: {
				code: 100,
				name: '待支付'
			},
			DSY: {
				code: 200,
				name: '待使用'
			},
			YYZ: {
				code: 300,
				name: '预约中'
			},
			FWZ: {
				code: 400,
				name: '服务中'
			},
			YWC: {
				code: 500,
				name: '交易完成'
			},
			YGB: {
				code: 600,
				name: '交易关闭'
			}
		}
	},
	
	// 售后状态定义
	serviceStatus() {
		return {
			ZZSHZ: {
				code: 1,
				name: '正在审核中'
			},
			SHWTG: {
				code: 2,
				name: '审核未通过'
			},
			SHYTG: {
				code: 3,
				name: '审核已通过'
			},
			DSMQJ: {
				code: 4,
				name: '待上门取件'
			},
			SMQJZ: {
				code: 5,
				name: '上门取件中'
			},
			DZXJH: {
				code: 6,
				name: '待自行寄回'
			},
			DSJSH: {
				code: 7,
				name: '待商家收货'
			},
			ZZHHZ: {
				code: 8,
				name: '正在换货中'
			},
			SHYGB: {
				code: 9,
				name: '售后已关闭'
			},
			ZZTKZ: {
				code: 10,
				name: '正在退款中'
			},
			SHYWC: {
				code: 11,
				name: '售后已完成'
			},
		}
	},

	// 获取订单状态对应的中文名称
	statusName(status) {
		let res = XEUtils.find(order.status(), item => item.code == status)
		return res.name
	},
	
	// 获取订单状态对应的中文名称
	serviceStatusName(status) {
		let res = XEUtils.find(order.onlineServiceStatus(), item => item.code == status)
		return res.name
	},
	
	// 计算订单中包含的设备押金金额，返回null，表示没有设备押金
	countOrderDeposit(goodsList) {
		if (!goodsList || goodsList.length <= 0) {
			return null
		}
		
		let deposit = 0
		
		let hasLease = false
		
		goodsList.forEach(item => {
			if (item.goodsBase && item.goodsBase.type == 'lease') {
				hasLease = true
				deposit += order.countGoodsDeposit(item.skuInfo)
			}
		})
		
		if (!hasLease) {
			return null
		}
		
		return utils.formatPrice(deposit)
	},
	
	// 获取商品SKU名称
	getSkuName(skuInfo) {
		let res = order.getGoodsSku(skuInfo)
		if (!res) {
			return null
		}
		
		if (!skuInfo.goodsSku.specList || skuInfo.goodsSku.specList.length <= 0) {
			return null
		}
		
		let specIds = res.specIds
		let result = []
		
		skuInfo.goodsSku.specList.forEach(spec => {
			spec.list.forEach(spec_item => {
				if (XEUtils.findIndexOf(specIds, item => item == spec_item.id) != -1) {
					result.push(spec.name  + '：' + spec_item.name)
				}
			})
		})
		
		if (result.length <= 0) {
			return null
		}
		
		return result.join('；')
	},
	
	// 获取商品选择的SKU
	getGoodsSku(skuInfo) {
		if (!skuInfo) {
			return null
		}
		
		let skuId
		
		if (skuInfo.selectedGoodsSpec.skuId != undefined) {
			skuId = skuInfo.selectedGoodsSpec.skuId
		}else if (skuInfo.selectedGoodsSpec.id != undefined) {
			skuId = skuInfo.selectedGoodsSpec.id
		}
		
		let res = XEUtils.find(skuInfo.goodsSku.skuList, item => item.skuId == skuId)
		return res
	},
	
	// 获取定制服务商品SKU名称
	getCustomSkuName(specList, skuList, skuId) {
		if (!specList || specList.length <= 0) {
			return null
		}
		let res = order.getCustomGoodsSku(skuList, skuId)
		if (!res) {
			return null
		}
		
		let specIds = res.specIds
		let result = []
		
		specList.forEach(spec => {
			spec.list.forEach(spec_item => {
				if (XEUtils.findIndexOf(specIds, item => item == spec_item.id) != -1) {
					result.push(spec.name  + '：' + spec_item.name)
				}
			})
		})
		
		if (result.length <= 0) {
			return null
		}
		
		return result.join('；')
	},
	
	// 获取定制服务商品选择的SKU
	getCustomGoodsSku(skuList, skuId) {
		if (!skuList | !skuId) {
			return null
		}
		let res = XEUtils.find(skuList, item => item.skuId == skuId)
		return res
	},
	
	// 计算商品设备押金金额
	countGoodsDeposit(skuInfo) {
		let res = order.getGoodsSku(skuInfo)
		if (!res) {
			return 0
		}
		return skuInfo.selectedGoodsSpec.stock * res.priceInfo.deposit
	},
	
	// 获取定制服务、在线咨询服务、特色服务列表
	getCustomExtendSpecList(skuInfo) {
		if (!skuInfo) {
			return null
		}
		
		let result = []
		
		if (skuInfo.selectedCustomSpec && skuInfo.selectedCustomSpec.length > 0) {
			skuInfo.selectedCustomSpec.forEach(selectSpec => {
				skuInfo.goodsSku.customSpec.forEach(spec => {
					spec.list.forEach(spec_item => {
						if (selectSpec.id == spec_item.id) {
							let group = XEUtils.find(result, item => item.id == spec.id)
							if (!group) {
								group = {
									id: spec.id,
									name: spec.name,
									type: 'custom',
									list: []
								}
								result.push(group)
							}
							group.list.push({
								specItem: spec_item,
								specSku: order.getCustomGoodsSku(spec_item.goodsSku.skuList, selectSpec.skuId),
								specSkuName: order.getCustomSkuName(spec_item.goodsSku.specList, spec_item.goodsSku.skuList, selectSpec.skuId),
								stock: selectSpec.stock,
								isInvalid: selectSpec.isInvalid == 1
							})
						}
					})
				})
			})
		}
		
		if (skuInfo.selectedServiceSpec && skuInfo.selectedServiceSpec.length > 0) {
			skuInfo.selectedServiceSpec.forEach(selectSpec => {
				skuInfo.goodsSku.serviceSpec.forEach(spec => {
					spec.list.forEach(spec_item => {
						if (selectSpec.id == spec_item.id) {
							let group = XEUtils.find(result, item => item.id == spec.id)
							if (!group) {
								group = {
									id: spec.id,
									name: spec.name,
									type: 'service',
									list: []
								}
								result.push(group)
							}
							group.list.push({
								specItem: spec_item,
								specSku: order.getCustomGoodsSku(spec_item.goodsSku.skuList, selectSpec.skuId),
								specSkuName: order.getCustomSkuName(spec_item.goodsSku.specList, spec_item.goodsSku.skuList, selectSpec.skuId),
								stock: selectSpec.stock,
								isInvalid: selectSpec.isInvalid == 1
							})
						}
					})
				})
			})
		}
		
		if (skuInfo.selectedExtendSpec && skuInfo.selectedExtendSpec.length > 0) {
			skuInfo.selectedExtendSpec.forEach(selectSpec => {
				skuInfo.goodsSku.extendSpec.forEach(spec => {
					spec.list.forEach(spec_item => {
						if (selectSpec.id == spec_item.id) {
							let group = XEUtils.find(result, item => item.id == spec.id)
							if (!group) {
								group = {
									id: spec.id,
									name: spec.name,
									type: 'extend',
									list: []
								}
								result.push(group)
							}
							group.list.push({
								specItem: spec_item,
								specSku: order.getCustomGoodsSku(spec_item.goodsSku.skuList, selectSpec.skuId),
								specSkuName: order.getCustomSkuName(spec_item.goodsSku.specList, spec_item.goodsSku.skuList, selectSpec.skuId),
								stock: selectSpec.stock,
								isInvalid: selectSpec.isInvalid == 1
							})
						}
					})
				})
			})
		}
		
		// console.log(result)
		
		return result
	},
	
	// 获取售后按钮文本
	getServiceBtnText(afterServiceInfo) {
		if (!afterServiceInfo || afterServiceInfo.isService != 1) {
			return '申请售后'
		}
		
		let type = '售后'
		
		if (afterServiceInfo.type == 0) {
			type = '退款'
		}else if (afterServiceInfo.type == 1) {
			type = '退货退款'
		}else if (afterServiceInfo.type == 2) {
			type = '换货'
		}
		
		let status = '中'
		
		if (afterServiceInfo.status == 9) {
			status = '关闭'
		}else if (afterServiceInfo.status == 11) {
			status = '完成'
		}
		
		return type + status
	},
	
	// 获取购买的商品应该显示的SKU图片
	getBuyGoodsSkuImage(goodsBase, skuInfo) {
		let res = order.getGoodsSku(skuInfo)
		
		if (res) {
			if (res.image && res.image.length > 0) {
				return res.image
			}else {
				for (let i = skuInfo.goodsSku.specList.length - 1; i >= 0; i--) {
					for(let spec of skuInfo.goodsSku.specList[i].list) {
						if (spec.image && spec.image.length > 0 && XEUtils.findIndexOf(res.specIds, item => item == spec.id) != -1) {
							return spec.image
						}
					}
				}
			}
		}
		
		return goodsBase.mainImage
	},
	
	getSimpleSubOrderGoodsCount(goodsList) {
		let total = 0
		
		// 计算商品列表中的商品数量
		if (goodsList && goodsList.length > 0) {
			goodsList.forEach(goods => {
				// 计算商品购买数量
				total += parseInt(goods.buycount)
				
				// 计算附加商品规格购买数量
				if (goods.customList && goods.customList.length > 0) {
					goods.customList.forEach(spec => {
						if (spec && spec.goodsList && spec.goodsList.length > 0) {
							spec.goodsList.forEach(specItem => {
								total += parseInt(specItem.buycount)
							})
						}
					})
				}
				
				// 计算附加服务规格购买数量
				if (goods.serviceList && goods.serviceList.length > 0) {
					goods.serviceList.forEach(spec => {
						if (spec && spec.goodsList && spec.goodsList.length > 0) {
							spec.goodsList.forEach(specItem => {
								total += parseInt(specItem.buycount)
							})
						}
					})
				}
				
			})
		}
		
		return total
	},
	
	getSubOrderGoodsCount(goodsList) {
		let total = 0
		
		// 计算商品列表中的商品数量
		if (goodsList && goodsList.length > 0) {
			goodsList.forEach(goods => {
				// 计算商品购买数量
				total += parseInt(goods.skuInfo.selectedGoodsSpec.stock)
				
				// 计算附加规格购买数量
				let specList = order.getCustomExtendSpecList(goods.skuInfo)
				if (specList && specList.length > 0) {
					specList.forEach(spec => {
						if (spec && spec.list && spec.list.length > 0) {
							spec.list.forEach(specItem => {
								if (!specItem.isInvalid) {
									//附加规格不是无货状态，才计算附加规格数量
									total += parseInt(specItem.stock)
								}
							})
						}
					})
				}
			})
		}
		return total
	},
	
	getBlendServiceOrderGoodsCount(goodsInfo) {
		let total = 0
		
		if (goodsInfo) {
			total += parseInt(goodsInfo.buycount)
			// 计算附加商品规格购买数量
			if (goodsInfo.customList && goodsInfo.customList.length > 0) {
				goodsInfo.customList.forEach(spec => {
					if (spec && spec.goodsList && spec.goodsList.length > 0) {
						spec.goodsList.forEach(specItem => {
							total += parseInt(specItem.buycount)
						})
					}
				})
			}
		}
		return total
	},
	
	getCreateOrderGoodsCount(orderInfo) {
		let total = 0
		
		// 计算商品订单列表中的商品数量
		if (orderInfo && orderInfo.tempSubOrderList && orderInfo.tempSubOrderList.length > 0) {
			orderInfo.tempSubOrderList.forEach(tempSubOrder => {
				if (tempSubOrder && tempSubOrder.goodsList) {
					total += order.getSubOrderGoodsCount(tempSubOrder.goodsList)
				}
			})
		}
		
		// 计算服务订单列表中的服务数量
		if (orderInfo && orderInfo.tempServiceOrderList && orderInfo.tempServiceOrderList.length > 0) {
			orderInfo.tempServiceOrderList.forEach(tempSubOrder => {
				if (tempSubOrder && tempSubOrder.goodsInfo && tempSubOrder.goodsInfo.skuInfo && tempSubOrder.goodsInfo.skuInfo.selectedGoodsSpec) {
					total += parseInt(tempSubOrder.goodsInfo.skuInfo.selectedGoodsSpec.stock)
				}
			})
		}
		
		return total
	}
	
}

export default order
